package com.yt.ds.chapter4;

/**
 * @Author: lzf
 * @Date: 2020/7/31 16:44
 * @Description: 加权边API
 */
public class Edge implements Comparable<Edge>,IEdge {
    private final int v;    // 顶点1
    private final int w;    // 顶点2
    private final double weight;    // 边的权重

    public Edge(int v, int w, double weight) {
        this.v = v;
        this.w = w;
        this.weight = weight;
    }

    public double getWeight() {
        return weight;
    }

    public int either() {
        return v;
    }

    // 另一个顶点
    public int other(int x) {
        if (x == v) {
            return w;
        } else if (x == w) {
            return v;
        } else {
            throw new RuntimeException("Inconsistent edge");
        }
    }

    @Override
    public int compareTo(Edge o) {
        return Double.compare(this.weight, o.getWeight());
    }

    @Override
    public String toString() {
        return String.format("%d-%d %.2f", v, w, weight);
    }
}
